/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log.nide.util.parser;

import java.io.FileReader;
import org.apache.commons.io.IOUtils;
import org.b3log.nide.util.parser.java.NideJavaParser;
import java.lang.reflect.Type;
import java.util.Set;
import java.util.List;
import java.lang.reflect.Method;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

/**
 * {@link NideJavaParser} test case.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.4, Aug 13, 2010
 */
public final class NideJavaParserTestCase {

    /**
     * Nide Java parser.
     */
    private NideJavaParser nideJavaParser;

    /**
     * Initializes test instance.
     *
     * @throws Exception exception
     */
    @BeforeTest
    public void beforeTest() throws Exception {
        final String testDataFilePath =
                NideJavaParserTestCase.class.getClassLoader().
                getResource("HelloWorld.java").toURI().getPath();
        final String javaSourceCode = IOUtils.toString(
                new FileReader(testDataFilePath));
        nideJavaParser = new NideJavaParser(javaSourceCode);
    }

    /**
     * Tests {@linkplain NideJavaParser#getImportNames()} method.
     */
    @Test
    public void getImportNames() {
        final List<String> importNames = nideJavaParser.getImportNames();
        final String[] names = importNames.toArray(new String[0]);
        assertEquals(names[0], "java.lang.reflect.Type");
        assertEquals(names[1], "java.util.Map");
        assertEquals(names[2], "org.testng.Assert.*");
    }

    /**
     * Tests {@linkplain NideJavaParser#getLocalVariableType(java.lang.String, int, int) }
     * method.
     */
    @Test
    public void getLocalVariableType() {
        final int stringLineNum = 23, stringEndColumnNum = 10;
        final Class<?> string =
                nideJavaParser.getLocalVariableType(stringLineNum,
                                                    stringEndColumnNum);
        assertEquals(string, String.class);

        final int setLineNum = 35, setEndColumnNum = 12;
        final Class<?> set =
                nideJavaParser.getLocalVariableType(setLineNum, setEndColumnNum);
        assertEquals(set, Set.class);
    }

    /**
     * Tests {@linkplain NideJavaParser#getMethods(int, int)} method.
     */
    @Test
    public void getMethods() {
        final int stringLineNum = 23;
        final int stringEndColumnNum = 10;
        final Class<?> string =
                nideJavaParser.getLocalVariableType(stringLineNum,
                                                    stringEndColumnNum);
        assertEquals(string, String.class);
        Method[] methods = nideJavaParser.getMethods(stringLineNum,
                                                     stringEndColumnNum);
        assertEquals(methods, String.class.getMethods());

        final int setLineNum = 35, setEndColumnNum = 12;
        final Class<?> set =
                nideJavaParser.getLocalVariableType(setLineNum, setEndColumnNum);
        assertEquals(set, Set.class);
        methods = nideJavaParser.getMethods(setLineNum, setEndColumnNum);
        assertEquals(methods, Set.class.getMethods());

        final int typeLineNum = 40, typeEndColumnNum = 10;
        final Class<?> type =
                nideJavaParser.getLocalVariableType(typeLineNum,
                                                    typeEndColumnNum);
        assertEquals(type, Type.class);
        methods = nideJavaParser.getMethods(typeLineNum, typeEndColumnNum);
        assertEquals(methods, Type.class.getMethods());
    }
}
